<%@page import="com.carnation.utils.MenuFileUtril"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="taglibs.jsp"%>
<%@ include file="easyuiLib.jsp"%>
<% MenuFileUtril menuFileUtril = new MenuFileUtril(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>FTP管理页面</title>
<script type="text/javascript">
	$(function(){
		$('#local').datagrid({
			url:'${ctx}/localFile',
			fitColumns:'true',
			idField:'name',
			rownumbers:'true',
			singleSelect:'true',
			columns:[[
			          {field:'fileName',title:'文件名称',width:100},
			          {field:'size',title:'文件大小',width:100},
			          {field:'fileType',title:'文件类型',width:100},
			          {field:'lastModify',title:'上传时间',width:100}
			      ]],
			toolbar:[{
				text:'返回',
				iconCls:'icon-redo',
				handler:function(){
					var path = $('#lpath').textbox('getValue');
					if(path == "/"){
						$.messager.alert('提示','本目录未根目录');	
					}
					else{
						var paths = path.split("/");
						var newpath = "";
						for(var i=0;i<paths.length -1;i++){
							newpath = newpath + "/" + paths[i];	
						}
						$('#local').datagrid('load',{
							path:newpath
						});
						$('#lpath').textbox('setValue',newpath);
					}
				}
			},'-',{
				text:'下载',
				iconCls:'icon-save',
				handler:function(){
					var row = $('#local').datagrid('getSelected');
					console.log(row);
					if(row == null){
						$.messager.alert('提示','请选择要下载的文件')
					}
					else{
						if(row.fileType == "文件"){
							var url = "${ctx}/downLocalFile";
							var path = $('#lpath').textbox('getValue');
							var params = {}; 
							params.path = path;
							params.filename = row.fileName;
							_download(url,params);
						}
						else{
							$.messager.alert('提示','你选择的并非文件,请重新选择');
						}
					}
				}
			},'-',{
				text:'上传',
				iconCls:'icon-save',
				handler:function(){
					var row = $('#local').datagrid('getSelected');
					if(row == null){
						$.messager.alert('提示','请选择需要上传的文件');
					}
					else{
						if(row.fileType != "文件"){
							$.messager.alert('提示','请选择上传的文件');
						}
						else{
							var localp = $('#lpath').textbox('getValue');
							var remotep = $('#rpath').textbox('getValue');
							var filename = row.fileName;
							$.ajax({
								url: '${ctx}/uploadFTP',
								type:'POST',
								async:false,
								data:'localpath='+localp + "&remotepath=" + remotep + '&filename=' + filename,
								success:function(data){
									var json = JSON.parse(data);
									$.messager.alert('结果',json.msg);
								}
							});
							$('#remote').datagrid('load',{
								path:remotep
							});
						}
					}
				}
			}],
			onDblClickRow:function(index,row){
				console.log(row);
				var path = $('#lpath').textbox('getValue');
				if(row.fileName == ".." && path != "/"){
					var paths = path.split("/");
					var parentPath = "";
					for(var i=0;i<paths.length-1;i++){
						parentPath = parentPath + "/" + paths[i];
					}
					$('#lpath').textbox('setValue',parentPath);
					$('#local').datagrid('load',{
						path:parentPath
					});
				}
				else if(row.fileName == ".." && path == "/"){
					$.messager.alert('提示','本目录为根目录');
				}
				else{
					if(row.fileType == "文件夹"){
						var newpath = "";
						if(path != "/"){
							newpath = path + "/" + row.fileName;
						}
						else{
							newpath= "/" + row.fileName;
						}
						console.log(newpath);
						$('#local').datagrid('load',{
							path:newpath
						});
						$('#lpath').textbox('setValue',newpath);
					}
					else if(row.fileType == "文件"){
						var url = "${ctx}/downLocalFile";
						var params = {};
						params.path = path;
						params.filename = row.fileName;
						_download(url,params);
					}
					else{
						console.log("未知类型");
					}
				}
			}
		});
		
		$('#remote').datagrid({
			url:'${ctx}/remoteFile',
			fitColumns:'true',
			idField:'fileName',
			rownumbers:'true',
			singleSelect:'true',
			columns:[[
			          {field:'fileName',title:'文件名称',width:100},
			          {field:'fileSize',title:'文件大小',width:100},
			          {field:'fileType',title:'文件类型',width:100},
			          {field:'fileLastAccess',title:'最后修改时间',width:100},
			          {field:'filePriviledge',title:'权限',width:100},
			          {field:'fileOwner',title:'所有者',width:100}
			      ]],
			toolbar:[{
				text:'返回',
				iconCls:'icon-redo',
				handler:function(){
					var path = $('#rpath').textbox('getValue');
					if(path == "/"){
						$.messager.alert('提示',"根目录无法返回");
					}
					else{
						var newpath = "";
						var p = path.split("/");
						for(var i=0;i<p.length-1;i++){
							newpath = newpath + "/" + p[i];
						}
						console.log(newpath);
						$('#remote').datagrid('load',{
							path:newpath
						});
						$('#rpath').textbox('setValue',newpath);
					}
				}
			},'-',{
				text:'下载',
				iconCls:'icon-save',
				handler:function(){
					var row = $('#remote').datagrid('getSelected');
					console.log(row);
					if(row == null){
						$.messager.alert("提示","请选择要下载的文件");
					}
					else{
						if(row.fileType == "文件"){
							var path = $('#rpath').textbox('getValue');
							var url = "${ctx}/downRemoteFile";
							var params = {};
							params.path = path;
							params.filename = row.fileName;
							_download(url,params);
						}
						else{
							$.messager.alert("提示","你所选择的并非文件");
						}
					}
				}
			},'-',{
				text:'删除',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#remote').datagrid('getSelected');
					console.log(row);
					if(row == null){
						$.messager.alert("提示","请选择要删除的文件");
					}
					else{
						if(row.fileType == "文件" || row.fileType == "文件夹" ){
							$.messager.confirm('确认','确定删除' + row.fileName + '文件吗？',function(r){
								if(r){
									var path = $('#rpath').textbox('getValue');
									$.ajax({
										url:"${ctx}/deleteRemoteFile",
										async : false,
										data : "path="+path +"&filename=" + row.fileName,
										type : "POST",
										success : function(data){
											var json = JSON.parse(data);
											$.messager.alert('结果',json.msg);
										}
									});
									$('#remote').datagrid('load',{
										path:path
									});
								}
							});
						}
						else{
							$.messager.alert("提示","你所选择的并非文件或文件夹");
						}
					}
				}
			}],
			onDblClickRow:function(index,row){
				console.log(row);
				var path = $('#rpath').textbox('getValue');
				if(row.fileName == ".." && path == "/"){
					$.messager.alert('提示',"根目录不能返回上层目录");
				}
				else if(row.fileName == ".." && path != "/"){
					var p = path.split("/");
					var newpath="";
					for(var i=0;i<p.length -1;i++){
						newpath = newpath + "/" + p[i];
					}
					console.log(newpath);
					$('#rpath').textbox('setValue',newpath);
					$('#remote').datagrid('load',{
						path:newpath
					});
				}
				else{
					if(row.fileType == "文件夹"){
						var newpath="";
						if(path != "/"){
							newpath = path + "/" + row.fileName;
						}
						else{
							newpath= "/" + row.fileName;
						}
						console.log(newpath);
						$('#rpath').textbox('setValue',newpath);
						$('#remote').datagrid('load',{
							path:newpath
						});
					}
					else if(row.fileType == "文件"){
						$.messager.confirm('确认','确认下载该文件吗？',function(r){
							if(r){
								var url = "${ctx}/downRemoteFile";
								var params ={};
								params.path = path;
								params.filename = row.fileName;
								_download(url,params);
							}
						})
					}
					else{
						$.messager.alert('提示','出错了，请联系管理员');
					}
				}
			}
		});
		
	});
	
	function _download(url,params){
		var _form = $('#download');
    	if(_form){
    		_form.remove();
    	}
        _form = $('<form id="download" ' + 'action=' +url +
					' method="post" target="_blank" style="">' +
					'</form>');
		$.each(params, function(index, val) {
		   _form.append('<input type="hidden" value=' + val+
					' name="'+index+'" id="'+index+'"/>');
		});
		$("body").append(_form);
		_form.submit();
	}
</script>
</head>
<body class="easyui-layout">
	<div region="west" width="50%" title="应用端" collapsible="false">
		<table style="width:100%">
			<tr>
				<td style="width:10%">路径:</td>
				<td style="width:90%"><input id="lpath" readOnly="true" class="easyui-textbox" style="width:100%" value="/" /></td>
			</tr>
		</table>
		<table id ="local"></table>
	</div>
	<div region="east" width="50%" title="FTP端" collapsible="false">
		<table style="width:100%">
			<tr>
				<td style="width:10%">路径:</td>
				<td style="width:90%"><input id="rpath" readOnly="true" class="easyui-textbox" style="width:100%" value="/" /></td>
			</tr>
		</table>
		<table id="remote"></table>
	</div>
	<div region="center"></div>
</body>
</html>